/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.startup;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author bfindeisen
 */
public class StartApp {

    private static StartupSplashScreen sp;

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                StartUpProperties prop = StartUpProperties.getInstance();

                setLaf();

                final StartupFrame frame = new StartupFrame();
                switch (prop.getType()) {
                    case BOTH:

                        if (prop.hasProps()) {
                            frame.setBothSelected();
                        }

                        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
                        Rectangle window = frame.getBounds();
                        frame.setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
                        frame.setVisible(true);
                        break;
                    case MPD:
                        sp = new StartupSplashScreen(StartUpProperties.StartupType.MPD);
                        break;
                    case SQUEEZE:
                        sp = new StartupSplashScreen(StartUpProperties.StartupType.SQUEEZE);
                        break;
                }
            }
        });
    }

    public static void runSqueezeCenter() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                setLaf();
                com.bff.slimserver.musicjukebox.lib.StartSqueeze.runApp();
            }
        }).start();
    }

    public static void runMPD() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                setLaf();
                com.bff.mpd.musicjukebox.lib.StartMPD.runApp();
            }
        }).start();
    }

    private static void setLaf() {
        try {
            UIManager.setLookAndFeel(StartUpProperties.getInstance().getLAFClassName());
            UIManager.put("Slider.paintValue", Boolean.FALSE);
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex1) {
                Logger.getLogger(StartApp.class.getName()).log(Level.SEVERE, null, ex1);
            } catch (InstantiationException ex1) {
                Logger.getLogger(StartApp.class.getName()).log(Level.SEVERE, null, ex1);
            } catch (IllegalAccessException ex1) {
                Logger.getLogger(StartApp.class.getName()).log(Level.SEVERE, null, ex1);
            } catch (UnsupportedLookAndFeelException ex1) {
                Logger.getLogger(StartApp.class.getName()).log(Level.SEVERE, null, ex1);
            }

            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public static boolean restartApp() {
        String[] command = new String[1];

        if (isWindows()) {
            command[0] = "start.bat";
        } else if (isUnix()) {
            command[0] = "./startBJJ.sh";
        } else {
            return false;
        }
        try {
            Runtime.getRuntime().exec(command);
        } catch (IOException ex) {
            Logger.getLogger(StartApp.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.exit(0);
        return true;
    }

    public static boolean isWindows() {
        String os = System.getProperty("os.name").toLowerCase();
        //windows
        return (os.indexOf("win") >= 0);

    }

    public static boolean isMac() {
        String os = System.getProperty("os.name").toLowerCase();
        //Mac
        return (os.indexOf("mac") >= 0);

    }

    public static boolean isUnix() {
        String os = System.getProperty("os.name").toLowerCase();
        //linux or unix
        return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);

    }
}
